<!--
@name:addDialog
@author:Zhenglei
@date:15:00
@description:addDialog
@update:2022/5/715:00
-->
<template>
  <div>
    <el-dialog :visible.sync="visible"
               title="添加医院"
               width="30%"
               center
               :before-close="modalClose"
               :close-on-click-modal="false"
               :close-on-press-escape="false">
      <el-form :model="submitData"
               ref="formModelRef"
               label-width="180px"
               :rules='formModelRules'>
        <el-form-item label="选择医院：" prop="hospitalId">
          <el-select v-model="submitData.hospitalId"
                     class="w-200"
                     placeholder="选择医院">
            <el-option v-for="item in hospitalList"
                       :key="item.hospitalId"
                       :label="item.name"
                       :value="item.hospitalId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否包含医联体单位：" prop="isContainUnion">
          <el-radio-group v-model="submitData.isContainUnion">
            <el-radio :label="0">否</el-radio>
            <el-radio :label="1">是</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item class="is-required" v-for="(item,index) in itemPirceList" :key="index"
                      :label="item.itemPrice+'服务费：'">
          <el-input class="w-200" v-model="item.serviceFee" clearable
                    placeholder="请输入服务费"/>
        </el-form-item>
      </el-form>
      <p align="center">
        <el-button @click="modalClose"
                   type="info">取 消
        </el-button>
        <el-button type="primary"
                   @click="handleSubmit()">确 认
        </el-button>
      </p>

    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "addDialog",
  props: {
    dataObj: {
      type: Object,
      default: {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      submitData: {
        isContainUnion: 0,
        hospitalId: '',
        healthCheckPackageId: this.dataObj.id,
        cdpFeeDetailDtoList: []
      },
      hospitalList: [],
      itemPirceList: [],
      formModelRules: {
        hospitalId: [
          {required: true, message: '请选择医院'}
        ]
      },
    }
  },
  created() {
    this.getCompanyList()
    this.getPackageItemList()
  },
  methods: {
    modalClose() {
      this.$emit('update:visible', false); // 直接修改父组件的属性
    },
    handleSubmit() {
      this.$refs.formModelRef.validate(valid => {
        if (valid) {
          this.submitData.cdpFeeDetailDtoList = this.itemPirceList
          let allPrice = this.submitData.cdpFeeDetailDtoList.every(item => item.serviceFee);
          if (allPrice) {
            this.post(this.api.cooperationHospital.saveCDPFee, this.submitData).then(res => {
              if (res.data.code === '200') {
               this.$message.success('新增成功')
                this.modalClose()
                this.$parent.getList()
              } else {
                this.$Message.error(res.data.message);
              }
            });
          } else {
            this.$message.error('请输入价格')
          }
        }
      })

      console.log("提交", this.submitData)
    },
    //查询医院
    getCompanyList() {
      this.post(this.api.company.getHospitalList, {
        pageSize: 200
      }).then(res => {
        if (res.data.code === '200') {
          this.hospitalList = res.data.object;
        } else {
          this.$Message.error(res.data.message);
        }
      });
    },
    //查询配置
    getPackageItemList() {
      this.post(this.api.cooperationHospital.getPackageItemList, {
        healthCheckPackageId: this.dataObj.id
      }).then((res) => {
        if (res.data.code === '200') {
          this.itemPirceList = res.data.object
          this.itemPirceList.forEach(item => {
            this.$set(item, 'serviceFee', '')
          })
        }
      })
    },
  },
}
</script>

<style scoped>

</style>
